package com.example.hrsystem.workflow.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.WorkflowViewModel;
import com.example.hrsystem.workflow.model.ProcessDefinition;

import java.util.List;

/**
 * 流程定义列表界面Activity
 * 展示所有流程定义列表和相关操作
 */
public class ProcessDefinitionListActivity extends AppCompatActivity {
    private WorkflowViewModel workflowViewModel;
    private RecyclerView recyclerView;
    private ProcessDefinitionAdapter adapter;
    private TextView emptyView;
    private Button addButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_definition_list);

        // 初始化ViewModel
        workflowViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WorkflowViewModel.class);

        // 初始化UI组件
        initUI();

        // 设置观察者
        setupObservers();

        // 加载数据
        workflowViewModel.loadAllProcessDefinitions();
    }

    private void initUI() {
        recyclerView = findViewById(R.id.recyclerView_process_definitions);
        emptyView = findViewById(R.id.text_empty);
        addButton = findViewById(R.id.button_add_process_definition);

        // 设置RecyclerView
        adapter = new ProcessDefinitionAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        // 设置添加按钮点击事件
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到流程定义详情页面（新建）
                Intent intent = new Intent(ProcessDefinitionListActivity.this, ProcessDefinitionDetailActivity.class);
                intent.putExtra("IS_NEW", true);
                startActivity(intent);
            }
        });

        // 设置列表项点击事件
        adapter.setOnProcessDefinitionClickListener(new ProcessDefinitionAdapter.OnProcessDefinitionClickListener() {
            @Override
            public void onProcessDefinitionClick(ProcessDefinition processDefinition) {
                // 跳转到流程定义详情页面（编辑）
                Intent intent = new Intent(ProcessDefinitionListActivity.this, ProcessDefinitionDetailActivity.class);
                intent.putExtra("IS_NEW", false);
                intent.putExtra("PROCESS_DEFINITION_ID", processDefinition.getId());
                startActivity(intent);
            }
        });
    }

    private void setupObservers() {
        // 观察流程定义列表数据
        workflowViewModel.getAllProcessDefinitions().observe(this, new androidx.lifecycle.Observer<List<ProcessDefinition>>() {
            @Override
            public void onChanged(List<ProcessDefinition> processDefinitions) {
                if (processDefinitions != null && !processDefinitions.isEmpty()) {
                    adapter.setProcessDefinitions(processDefinitions);
                    recyclerView.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}